**********************
*Create JSCReg sample
**********************
set more off
*use hiding variables from network analysis
use "Network Data/DUP_hiding_04012004_JSCReg_only.dta",clear
gen year=2004
append using "Network Data/DUP_hiding_11102006_JSCReg_only.dta"
replace year=2006 if year==.

*merge with data on presence of each firm in Delo, UP or both
merge m:1 okpo using "Prepared Data/Delo_UP_presence.dta"
drop _merge
sort okpo year

*assign political affiliation dummy: 1-blue, 2-orange, 3-gray
gen color=1 if  group=="Derkach" | group=="Energo" | group=="Franchuk" | group=="Interpipe" | group=="Khmelnytskyi" | group=="Kirpa" | group=="Kliuev" | group=="Kyiv-Seven" | group=="Old Donetsk" | group=="Radon" | group=="SCM" | group=="Tabachnyk" | group=="TAS" | group=="Ukrinterproduct" //14
replace color=2 if group=="Aval" | group=="Brinkford" | group=="Finansy i Kredyt" | group=="Ignatenko" | group=="Ivchenko" | group=="Omelchenko" | group=="Orlan" | group=="Rymaruk" | group=="Tretiakov" | group=="UkrPromInvest" | group=="Yushchenko" | group=="Zagreva" | group=="Pryvat" //15
replace color=3 if group=="Basis" | group=="Feldman" | group=="Intercontact"  | group=="ISD" | group=="Kahanovskyi" | group=="UkrSotsBank" | group=="UkrSybBank" | group=="double SCM/ISD" | group=="double ISD/Pryvat" //9

gen blue=1 if color==1
replace blue=0 if blue==.
gen orange=1 if color==2
replace orange=0 if orange==.
gen gray=1 if color==3
replace gray=0 if gray==.

*keep firms present in JSCReg
drop if present_JSCReg==0

*keep firms with sector and controls data
merge m:1 okpo using "Prepared Data/sector.dta"
keep if _merge==3
drop _merge
merge m:1 okpo  using "Prepared Data/econ.dta"
keep if _merge==3
drop _merge
merge m:1 okpo using "Prepared Data/privatized.dta"
keep if _merge==3
drop _merge
*add regions and IV data
merge m:1 okpo using "Prepared Data/IV.dta"
keep if _merge==3
drop _merge
*restrict sample to firms with economic data
keep if lemp!=. & tfp!=. & privatized!=. 

*Generate change in foreign and offshore
gen time=1 if year==2004
replace time=2 if year==2006

*keep only those firms that are present in 2004
bysort okpo: egen mtime=min(time)
keep if mtime==1
*===>8 firms are available in 2006 only, drop them

*generate DFO and DOFF for 253 firms present in both years
tsset okpo time
gen dfo=foreign_in_chain-l.foreign_in_chain
gen doff=offshore-l.offshore
bysort okpo: egen max_dfo=max(dfo)
bysort okpo: egen max_doff=max(doff)
drop dfo doff time mtime
rename max_dfo dfo
rename max_doff doff

save "Regressions Data/Delo_UP_Panel_Stezha_h0406.dta",replace

*drop firms with no owners in 2004
gen nown2004=1 if has_owners==0 & year==2004
bysort okpo: egen nown=max(nown2004)
drop if nown==1
save "Regressions Data/JSCReg0406_nown.dta",replace
